class User {
    constructor(store, options) {
        this.store = store;
        this.options = options;
        this.install();
        return this;
    }
    install() {
        this.store.registerModule(["user"], this.options.data());
        let obj = this.options.mixin();
        for (let n in obj) {
            if (n !== "data") this[n] = obj[n];
        }
        return this;
    }
    uninstall() {
        this.store.unregisterModule(["user"]);
        return this;
    }
}

export const user = function(Vue, store) {
    return new Promise((resolve, reject) => {
        try {
            Vue.prototype.$user = Vue.$user = new User(store, {
                data() {
                    return {
                        state: {
                            user: {
                                userInfo: {},
                            },
                        },
                        actions: {
                            getUserInfo(store) {
                                return new Promise((resolve, reject) => {
                                    store
                                        .dispatch("requestData", {
                                            path: "/login/getUserInfo",
                                            data: {},
                                        })
                                        .then((v) => {
                                            store.dispatch("saveUserInfo", {
                                                userInfo: v.data.userInfo,
                                            });
                                            resolve(v);
                                        })
                                        .catch((v) => {
                                            reject(v);
                                        });
                                });
                            },
                            hasUser(store, json) {
                                return new Promise((resolve, reject) => {
                                    store
                                        .dispatch("requestData", {
                                            path: "/login/hasUser",
                                            data: json,
                                        })
                                        .then((v) => {
                                            resolve(v);
                                        })
                                        .catch((v) => {
                                            reject(v);
                                        });
                                });
                            },
                            saveUserInfo(store, json) {
                                return new Promise((resolve, reject) => {
                                    try {
                                        store.commit("saveUserInfo", json);
                                        resolve();
                                    } catch (e) {
                                        reject(e);
                                    }
                                });
                            },
                            deleteUserInfo(store) {
                                return new Promise((resolve, reject) => {
                                    try {
                                        store.commit("deleteUserInfo");
                                        resolve();
                                    } catch (e) {
                                        reject(e);
                                    }
                                });
                            },
                        },
                        mutations: {
                            saveUserInfo(state, json) {
                                state.user.userInfo = json.userInfo;
                            },
                            deleteUserInfo(state) {
                                state.user.userInfo = {};
                            },
                        },
                    };
                },
                mixin() {
                    return {
                        hasUser(json) {
                            return this.store.dispatch("hasUser", json);
                        },
                        getUserInfo(json) {
                            return this.store.dispatch("getUserInfo", json);
                        },
                        deleteUserInfo(json) {
                            return this.store.dispatch("deleteUserInfo", json);
                        },
                    };
                },
            });
            resolve(Vue);
        } catch (e) {
            reject(e);
        }
    });
};